//
//  KRLTimelineCell.m
//  KaraLive
//
//  Created by Zhijia Zhong on 2018/9/14.
//  Copyright © 2018年 naxigoren. All rights reserved.
//

#import "KRLTimelineCell.h"
#import "KRLTimelineModel.h"
#import "KRLTypeView.h"
#import "KRLInteractionBar.h"

@interface KRLTimelineCell()

@property (nonatomic, strong) UIImageView *avatarView;
@property (nonatomic, strong) UILabel *nicknameLabel;
@property (nonatomic, strong) UIImageView *timeIcon;
@property (nonatomic, strong) UILabel *timeLabel;
@property (nonatomic, strong) UIImageView *thumbView;
@property (nonatomic, strong) UIImageView *playView;
@property (nonatomic, strong) KRLTypeView *typeView;
@property (nonatomic, strong) KRLInteractionBar *interactionBar;

@end

@implementation KRLTimelineCell

- (UIImageView *)avatarView {
    if (!_avatarView) {
        _avatarView = [[UIImageView alloc] init];
        _avatarView.layer.cornerRadius = 16;
        _avatarView.layer.masksToBounds = YES;
    }
    return _avatarView;
}

- (UILabel *)nicknameLabel {
    if (!_nicknameLabel) {
        _nicknameLabel = [[UILabel alloc] init];
        _nicknameLabel.textColor = [UIColor colorWithRGB:0x333333];
        _nicknameLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
    }
    return _nicknameLabel;
}

- (UIImageView *)timeIcon {
    if (!_timeIcon) {
        _timeIcon = [[UIImageView alloc] init];
        _timeIcon.image = [UIImage imageNamed:@"icon_time"];
    }
    return _timeIcon;
}

- (UILabel *)timeLabel {
    if (!_timeLabel) {
        _timeLabel = [[UILabel alloc] init];
        _timeLabel.textColor = [UIColor colorWithRGB:0xB3B3B3];
        _timeLabel.font = [UIFont systemFontOfSize:10 weight:UIFontWeightMedium];
    }
    return _timeLabel;
}

- (UIImageView *)thumbView {
    if (!_thumbView) {
        _thumbView = [[UIImageView alloc] init];
        _thumbView.contentMode = UIViewContentModeScaleAspectFill;
        _thumbView.layer.cornerRadius = 8;
        _thumbView.layer.masksToBounds = YES;
    }
    return _thumbView;
}

- (UIImageView *)playView {
    if (!_playView) {
        _playView = [[UIImageView alloc] init];
        _playView.image = [UIImage imageNamed:@"icon_record_play"];
    }
    return _playView;
}

- (KRLTypeView *)typeView {
    if (!_typeView) {
        _typeView = [[KRLTypeView alloc] init];
    }
    return _typeView;
}

- (KRLInteractionBar *)interactionBar {
    if (!_interactionBar) {
        _interactionBar = [[KRLInteractionBar alloc] init];
    }
    return _interactionBar;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self.contentView addSubview:self.avatarView];
        [self.contentView addSubview:self.nicknameLabel];
        [self.contentView addSubview:self.timeIcon];
        [self.contentView addSubview:self.timeLabel];
        [self.contentView addSubview:self.thumbView];
        [self.thumbView addSubview:self.interactionBar];
    }
    return self;
}

+ (BOOL)requiresConstraintBasedLayout {
    return YES;
}

- (void)updateConstraints {
    [self.avatarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(16);
        make.size.mas_equalTo(CGSizeMake(32, 32));
    }];
    
    [self.nicknameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.avatarView.mas_right).offset(8);
        make.centerY.mas_equalTo(self.avatarView);
    }];
    
    [self.timeIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(9.5, 9.5));
        make.centerY.mas_equalTo(self.avatarView);
        make.trailing.mas_equalTo(self.timeLabel.mas_leading).offset(-6);
    }];

    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.avatarView);
        make.trailing.mas_equalTo(-16);
    }];

    [self.thumbView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.right.mas_equalTo(-16);
        make.top.mas_equalTo(self.avatarView.mas_bottom).offset(8);
        make.height.mas_equalTo(self.thumbView.mas_width).multipliedBy(195.0 / 328);
    }];

    [self.interactionBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.height.mas_equalTo(34.5);
    }];

    [super updateConstraints];
}

- (void)updateCellWithModel:(KRLTimelineModel *)model {
    [self.avatarView sd_setImageWithURL:[NSURL URLWithString:model.avatar]];
    self.nicknameLabel.text = model.nickname;
    self.timeLabel.text = [NSString stringWithFormat:@"%f", model.createTime];
    [self.thumbView sd_setImageWithURL:[NSURL URLWithString:model.thumb]];
    self.interactionBar.likes = model.likes;
    self.interactionBar.comments = model.comments;
}

@end
